var contentid;
/*帮助中心**/
$(function(){
	showContent(getParamFromUrl());
	$(".prblOuter .leftNav ul.erJi li").click(function(){
		var thisnode=$(this);
		showContent(thisnode.children("a").attr("id").split('-')[1]);
	});
	
	$(".prblOuter .leftNav>ul>li").click(function(){
		$(this).children("ul").toggleClass("appr");
		$(this).children("h2").toggleClass("apr");
	});
	
})
function getParamFromUrl(){
	var indexContentId = window.location.href.split('=')[1]
	return indexContentId;
}
function showContent(thisnode){
	$("#mainResult").remove();
	var id;
	if(thisnode){
		if(isNaN(thisnode)){
			id=$(".contitles:first").attr("id").split('-')[1];
		}else{
			id=thisnode;
		}
	}else{
		id=$(".contitles:first").attr("id").split('-')[1];
	}
	$("#data-"+id).parents(".erJi").toggleClass("appr");
	$("#data-"+id).parents(".erJi").prev().toggleClass("apr");
	var layerIndex;
	 $.ajax({
         url: 'getContentByid',
         data: {id:id},
         type: 'post',
         dataType : 'json',
		 beforeSend : function () {
			 layerIndex = layer.load(1, {
				  shade: [0.1,'#fff'] //0.1透明度的白色背景
				});
         },
         success: function (data) {
            	 if(data.success && data.attributes && data.attributes.content) {
            		    $("#topText1").text(data.attributes.content.channel.channelName);
            		    $("#topText2").text(data.attributes.content.subTitle);
            		 	if(thisnode){
            		 		$("#data-"+id).parents(".erJi").addClass("appr");
            		 	}
            			$(".prblOuter .content h2").text(data.attributes.content.subTitle);
            			$("#mainContent").children().remove();
            			$("#mainContent").append(data.attributes.content.content);
            			$("#helpful").find(".shownum").text(data.attributes.content.helpful==null?0:data.attributes.content.helpful);
            			$("#helpless").find(".shownum").text(data.attributes.content.helpless==null?0:data.attributes.content.helpless);
					 	$("#helpful>button").attr('data-id', data.attributes.content.id);
					 	$("#helpless>button").attr('data-id', data.attributes.content.id);
            			$(".erJi a").removeClass("chose");
            			if(thisnode){
            				$("#data-"+id).addClass("chose"); 
            			}
            			//alert(2);
            			$(".content").show();
            	 }
         },
		 complete : function() {
			layer.close(layerIndex);
		 },
		 error:function(xx){
			console.log(xx)
		 }
     });
	 contentid=id;
}
function showResult(){
	var res=$("#wentiTest").val();
	if(res.trim()==""){
		layer.alert("请输入关键字");
		return false;
	}
	$("#mainResult").remove();
	var layerIndex;
	$.ajax({
        url: 'getContentByText',
        data: {text:res},
        type: 'post',
		beforeSend : function () {
			 layerIndex = layer.load(1, {
				  shade: [0.1,'#fff'] //0.1透明度的白色背景
				});
        },
        success: function (data) {
        	$("h2").removeClass("apr");
        	$("ul").removeClass("appr");
           	$(".content").hide();
           	$(".content").before(data);
        },
		complete : function() {
				layer.close(layerIndex);
		}
    });
}

function move(type, obj) {
	var canVote = true;	//是否能投票
	var id = $(obj).attr('data-id');
	var array = getHelpCenterCookie();
	if($.inArray(id, array) >= 0) {
		canVote = false;
	}
	if(!canVote) {
		layer.alert('此篇文章您已经投过票了，谢谢您的支持');
		return;
	}
	$.post("doaddOrMinHelp", { ishelpfulOrhelpless: type == 1 ? 'helpful' : 'helpless', id:contentid},function(data){
		if(data.success) {
			array.push(id);
			Cookies.set('helpCenter', JSON.stringify(array), {expires : 365});
			var x=".prblOuter .botNav button i" + (type == 1 ? '.yes.move' : '.no.move');
			$(x).show();
			$(x).animate({ 
		        right: "-100px",
		        top:"-200px",
		        opacity:"0.3"
		    }, 1000,function(){ 
		    	$(x).hide(); 
				$(x).css("top","-20px");
				$(x).css("right","105px");
				$(x).css("opacity","1"); 
				var num=parseInt($(x).siblings(".shownum").text())+1;
				$(x).siblings(".shownum").text(num);
		    }); 
		}
	},"json");
}

function getHelpCenterCookie() {
	var helpCenter = Cookies.get('helpCenter');
	if(helpCenter != null && helpCenter != '') {
		return JSON.parse(helpCenter);
	}
	return [];
}